package com.stone.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by stone on 17-5-27.
 */
@RestController
public class JsWebCatch {
    @RequestMapping(value = "jc")
    public void captureHtml(@RequestParam("ip") String ip) throws Exception {
        String strURL = "http://daletou.cjcp.com.cn/kaijiang/";
//        String strURL = "http://ip.chinaz.com/IP=" + ip1;
        URL url = new URL(strURL);
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        InputStreamReader input = new InputStreamReader(httpConn
                .getInputStream(), "utf-8");
        BufferedReader bufReader = new BufferedReader(input);
        String line = "";
        StringBuilder contentBuf = new StringBuilder();
        while ((line = bufReader.readLine()) != null) {
            contentBuf.append(line);
        }
        String buf = contentBuf.toString();
        System.out.println(buf);
        int beginIx = buf.indexOf("IP/服务器地址查询");
        int endIx = buf.indexOf("更多工具");
        String result = buf.substring(beginIx, endIx);
        System.out.println("captureHtml()的结果：\n" + result);
    }
}
